package com.tools.controller.user;

import com.tools.common.result.Result;
import com.tools.service.user.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/role")
@AllArgsConstructor
@Tag(name = "用户角色关联", description = "管理用户与角色的关联关系")
public class UserRoleController {

    private final UserRoleService userRoleService;

    @PostMapping
    @Operation(summary = "添加用户角色关联", description = "为用户分配角色")
    public Result<Void> addUserRole(
        @Parameter(description = "用户ID") @RequestParam Long userId,
        @Parameter(description = "角色ID") @RequestParam Long roleId) {
        return userRoleService.addUserRole(userId, roleId);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除关联关系", description = "根据关联ID解除用户角色绑定")
    public Result<Void> removeUserRole(
        @Parameter(description = "关联记录ID") @PathVariable Long id) {
        return userRoleService.removeUserRole(id);
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除关联", description = "删除多个用户角色关联记录")
    public Result<Void> batchRemoveUserRole(
        @Parameter(description = "关联ID列表") @RequestBody List<Long> ids) {
        return userRoleService.batchRemoveUserRole(ids);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "查询用户角色", description = "获取用户拥有的角色列表")
    public Result<List<Long>> getUserRoles(
        @Parameter(description = "用户ID") @PathVariable Long userId) {
        return userRoleService.getUserRoles(userId);
    }

    @GetMapping("/role/{roleId}")
    @Operation(summary = "查询角色用户", description = "获取拥有指定角色的用户列表")
    public Result<List<Long>> getRoleUsers(
        @Parameter(description = "角色ID") @PathVariable Long roleId) {
        return userRoleService.getRoleUsers(roleId);
    }
}
